﻿using gcloud_voice;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class GCVoiceUI : MonoBehaviour
{

	public Button Btn_Say;

	public Button Button_LocalFile;
	public Button Button_FileSvr;

	private string m_recordpath = string.Empty;
	private string m_downloadpath = string.Empty;

	public EventTrigger eventTrigger;
	// Use this for initialization
	void Start () {
		m_recordpath = Application.persistentDataPath + "/" + "recording.dat";
		m_downloadpath =  Application.persistentDataPath + "/" + "download.dat";
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.Init(IGCloudVoice.GCloudVoice.GetEngine());
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.onApplyMessageKeyComplete += (succ, code) =>
		{
			Debug.Log("onApplyMessageKeyComplete" + succ + "|" + code);
		};
		
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.onUploadReccordFileComplete += (succ, path, id) =>
		{
			Debug.Log("onUploadReccordFileComplete" + succ + "|" + path + "|" + id);
			GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.DownloadRecord(id, path);
		};

		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.onDownloadRecordFileComplete += (succ, path, id) =>
		{
			Debug.Log("onDownloadRecordFileComplete" + succ + "|" + path + "|" + id);
			if (succ)
				Button_FileSvr.gameObject.SetActive(true);
		};
		
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.onPlayRecordFileComplete += (succ, path) =>
		{
			Debug.Log("onPlayRecordFileComplete" + succ + "|" + path);
		};
		

		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.onSpeechToText += (succ, id, result) =>
		{
			Debug.Log("onSpeechToText" + succ + "|" + id + "|" + result);
		};
	}

	public void StartRecord()
	{
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.StartRecording(m_recordpath);
	}

	public void StopRecord()
	{
		var ret = GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.StopRecording();
		Button_LocalFile.gameObject.SetActive (true);
		if (ret)
			GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.UploadRecord(m_recordpath);
	}

	public void PlayRecord()
	{
		GVoiceSdk.GCloudVoiceAgent.OffLineVoiceAgent.PlayRecord(m_downloadpath);
	}
}
